package common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

@Configuration
public class RedisConfig {
	/**
	 * 注入 RedisConnectionFactory
	 */
	@Resource
	RedisConnectionFactory factory;

	/**
	 * 方法实现说明:实例化 RedisTemplate 对象
	 *
	 * @param
	 * @return org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.Object>
	 * @throws
	 * @author
	 */
	@Bean("redisTemplateForHash")
	public RedisTemplate<String, Object> redisTemplate() {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		// 开启事务
		//redisTemplate.setEnableTransactionSupport(true);
		redisTemplate.setConnectionFactory(factory);
		return redisTemplate;
	}

}